// index.ts
// 获取应用实例
const app = getApp<IAppOption>()
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'

Component({
  data: {
    mode: '',
    dateVisible: false,
    date: new Date('2021-12-23').getTime(), // 支持时间戳传入
    dateText: '',
    start: '2000-01-01 00:00:00',
    end: '2030-09-09 12:12:12',
    formData: {},
    fileList: [],
  },
  methods: {
    handleAdd(e) {
      const self = this
      const { fileList } = this.data;
      const { files } = e.detail;
      console.log(files)
      const file = files?.[0]
      wx.getFileSystemManager().readFile({
        filePath: file.url, //选择图片返回的相对路径
        encoding: 'base64', //编码格式
        success: function (res) {
          console.log(res.data)
          var base64 = 'data:image/png;base64,' + res.data
          self.setData({
            formData: {
              ...self.data.formData,
              picFile: base64,
              picFileName: file.name
            }
          })
        }
      })

      // 方法1：选择完所有图片之后，统一上传，因此选择完就直接展示
      this.setData({
        fileList: [...fileList, ...files],
      });
    },
    onUpload(file) {
      const { fileList } = this.data;

      this.setData({
        fileList: [...fileList, { ...file, status: 'loading' }],
      });
      const { length } = fileList;

      const task = wx.uploadFile({
        url: 'https://example.weixin.qq.com/upload', // 仅为示例，非真实的接口地址
        filePath: file.url,
        name: 'file',
        formData: { user: 'test' },
        success: () => {
          this.setData({
            [`fileList[${length}].status`]: 'done',
          });
        },
      });
      task.onProgressUpdate((res) => {
        this.setData({
          [`fileList[${length}].percent`]: res.progress,
        });
      });
    },
    handleRemove(e) {
      const { index } = e.detail;
      const { fileList } = this.data;

      fileList.splice(index, 1);
      this.setData({
        fileList,
      });
    },
    setFormValue(e) {
      // console.log(e)
      const id = e.currentTarget.dataset.name
      const value = e.detail.value
      this.setData({
        formData: {
          ...this.data.formData,
          [id]: value
        }
      })
    },
    onSubmit() {
      const token = wx.getStorageSync('token')

      console.log(this.data.formData)
      wx.request({
        header: {
          'Authorization': 'Bearer ' + token
        },
        url: `http://8.148.22.60:16060/app-api/dcert/application/create-person`,
        method: 'POST',
        data: this.data.formData,
        success: (res) => {
          console.log(res)
        }
      })
    },
    showPicker(e: any) {
      const { mode } = e.currentTarget.dataset;
      this.setData({
        mode,
        [`${mode}Visible`]: true,
      });
    },
    hidePicker() {
      const { mode } = this.data;
      this.setData({
        [`${mode}Visible`]: false,
      });
    },
    onConfirm(e: any) {
      const { value } = e.detail;
      const { mode } = this.data;

      console.log('confirm', value);

      this.setData({
        [mode]: value,
        [`${mode}Text`]: value,
      });

      this.hidePicker();
    },

    onColumnChange(e: any) {
      console.log('pick', e.detail.value);
    },
  },
})
